<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\ApiController;
use App\Libraries\Error;
use App\Models\Employee;
use App\Models\User;
use App\Models\WorkAttendance;
use Firebase\JWT\JWT;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class WorkAttendanceController extends ApiController
{


    public function create(Request $request) {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];



        if ( $request->id) {
            $work_attendance = WorkAttendance::findOrFail($request->id);
            $work_attendance->mtime = time();
        } else {
            $work_attendance = new WorkAttendance();
            $work_attendance->ctime = time();
        }
        $work_attendance->employee_id = $request->employee_id;
        $employee = Employee::where('employee_id',$request->employee_id)->where('company_id',$company_id)->where('status',1)->first();

        $work_attendance->company_id = $company_id;
        $work_attendance->name = $employee['name'];
        $work_attendance->organization_id = $employee['organization_id'];
        $work_attendance->shift_id = $request->shift_id;
        $work_attendance->clock_in = $request->clock_in;
        $work_attendance->accounting_statistics = $request->accounting_statistics;
        $work_attendance->daily_attendance = $request->daily_attendance;
        $work_attendance->first_stage_work = $request->first_stage_work;
        $work_attendance->first_stage_duty = $request->first_stage_duty;
        $work_attendance->second_stage_work = $request->second_stage_work;
        $work_attendance->second_stage_duty = $request->second_stage_duty;
        $work_attendance->third_stage_work = $request->third_stage_work;
        $work_attendance->third_stage_duty = $request->third_stage_duty;
        $work_attendance->uid = $uid;
        $work_attendance->status = $request->status?$request->status:1;

        $work_attendance->save();

        return showError(Error::ERR_OK,['id'=>$work_attendance->id]);

    }

    public function show(Request $request) {
        $work_attendance = WorkAttendance::findOrFail($request->id);
        return showError(Error::ERR_OK,['work_attendance'=>$work_attendance]);
    }

    public function index(Request $request)
    {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];
        // 搜索内容
        $where = [];
        $name = $request->input('name');
        if (!empty($name))
        {
            $where['name'] = $name;
        }

        $organization_id = $request->input('organization_id');
        if (!empty($organization_id))
        {
            $where['organization_id'] = explode(",",$organization_id);
        }

        $date = $request->input('date');
        if (!empty($date))
        {
            $where['date'] = $date;
        }


        $where['company_id'] = $company_id;
        $where['status'] = 1;

        $list = WorkAttendance::getList($where);

        foreach ($list as $k => &$v) {

            $v->employee_info = Employee::where('employee_id',$v->employee_id)->where('company_id',$company_id)->where('status',1)->first();
        }
        return showError(Error::ERR_OK,['list'=>$list]);
    }

    public function delete(Request $request) {
        WorkAttendance::where('id',$request->id)->update(['status'=>2,'mtime'=>time()]);
        return showError(Error::ERR_OK);
    }

    public function stat(Request $request) {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];
        $count = DB::select("SELECT status,count(*) as cnt FROM zlhr_work_attendance WHERE company_id = $company_id  GROUP BY status");
        return showError(Error::ERR_OK,['count'=>$count]);
    }


}
